package com.gzc.just.play.last.war.sceneserver.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * Redis缓存配置类
 * 
 * 配置不同类型数据的缓存策略和TTL（生存时间）
 * 作为Phase 4.3数据库持久化集成的一部分
 */
@Configuration
@EnableCaching
@ConditionalOnClass(RedisConnectionFactory.class)
@ConditionalOnProperty(name = "allinone.enable-redis", havingValue = "true", matchIfMissing = true)
public class RedisConfig {
    
    @Value("${lastwar.cache.player-cache-ttl:300}")
    private int playerCacheTtl;
    
    @Value("${lastwar.cache.npc-cache-ttl:600}")
    private int npcCacheTtl;
    
    @Value("${lastwar.cache.config-cache-ttl:3600}")
    private int configCacheTtl;
    
    @Value("${lastwar.cache.max-cache-size:10000}")
    private int maxCacheSize;
    
    /**
     * Redis缓存管理器配置
     * 设置不同类型数据的缓存策略
     */
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        // 默认缓存配置
        RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(10)) // 默认10分钟过期
                .serializeKeysWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new GenericJackson2JsonRedisSerializer()))
                .disableCachingNullValues(); // 不缓存null值
        
        // 特定缓存配置
        Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
        
        // 玩家数据缓存 - 5分钟过期
        cacheConfigurations.put("players", defaultConfig.entryTtl(Duration.ofSeconds(playerCacheTtl)));
        cacheConfigurations.put("player-attributes", defaultConfig.entryTtl(Duration.ofSeconds(playerCacheTtl)));
        cacheConfigurations.put("player-inventory", defaultConfig.entryTtl(Duration.ofSeconds(playerCacheTtl)));
        
        // NPC数据缓存 - 10分钟过期
        cacheConfigurations.put("npc-configs", defaultConfig.entryTtl(Duration.ofSeconds(npcCacheTtl)));
        cacheConfigurations.put("npc-instances", defaultConfig.entryTtl(Duration.ofSeconds(npcCacheTtl)));
        
        // 配置数据缓存 - 1小时过期
        cacheConfigurations.put("game-configs", defaultConfig.entryTtl(Duration.ofSeconds(configCacheTtl)));
        
        // 队伍数据缓存 - 5分钟过期
        cacheConfigurations.put("teams", defaultConfig.entryTtl(Duration.ofMinutes(5)));
        cacheConfigurations.put("team-members", defaultConfig.entryTtl(Duration.ofMinutes(5)));
        
        // 成就数据缓存 - 30分钟过期
        cacheConfigurations.put("player-achievements", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        
        // 战斗数据缓存 - 短期缓存（战斗进行中）
        cacheConfigurations.put("battles", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        cacheConfigurations.put("battle-participants", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        
        return RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(defaultConfig)
                .withInitialCacheConfigurations(cacheConfigurations)
                .build();
    }
}